/**
 * 系统变量注入器
 */
package cn.antia.cwin;

import java.util.Map;

import javax.servlet.http.*;

import cn.antia.cwin.Cele;
import cn.antia.cwin.page.StrutsAction;
import cn.antia.util.GoofyIdeas;
import cn.antia.cep.common.Privilege;

/**
 * @author sunkeei
 * @date Dec 3, 2007
 * @time 9:29:31 PM
 * @version 0.1
 */
public class ValiableInjector {
	/**
	 * 设置系统变量的值
	 * @param operateType 操作类型
	 * @param ecs 元素列表(ElementComsat)
	 * @param request HttpServletRequest
	 */
	public void setSysValiables(int operateType, Map<Cele,Object> ecs,HttpServletRequest request){
		Cele ec;
		String v;
        for (Map.Entry<Cele, Object> entry : ecs.entrySet()) {
            ec = entry.getKey();
			v = entry.getValue().toString();
			//前缀
			if(v != null && v.indexOf(SysConf.SYSTEM_VALIABLE_PREFIX) == 0){
				v = v.substring(SysConf.SYSTEM_VALIABLE_PREFIX.length());
				setValue(ec,v,request);
				continue;
			}
			//default
			if(operateType == StrutsAction.ADD){
				if(SysConf.isSystemString(ec.getField().getDefault())){
					setValue(ec,ec.getField().getDefault(),request);
					continue;
				}else if(entry.getValue() == null){
					ecs.put(ec,ec.getField().getDefault());
				}
			}
			//modify default
			if(operateType == StrutsAction.UPDATE){
				if(SysConf.isSystemString(ec.getField().getModifyValue())){
					setValue(ec,ec.getField().getModifyValue(),request);
				}else if(entry.getValue() == null){
					ecs.put(ec,ec.getField().getModifyValue());
				}
			}
		}
	}
	/**
	 * 设置一个元素的系统变量真实值
	 * @param ec ElementComsat
	 * @param sv valiable
	 * @param request HttpServletRequest
	 */
	protected void setValue(Cele ec,String sv,HttpServletRequest request){
		HttpSession session = request.getSession();
        /*
        if(sv.equals("SYS_TIME")){
			ec.setValue(GoofyIdeas.getNowDate("yyyy-MM-dd HH:mm:ss"));
		}else if(sv.equals("LOGIN_MANAGER_ID")){
			Privilege privilege = (Privilege)session.getAttribute("privilege");
			if(privilege != null && privilege.getUser() != null){
				ec.setValue(privilege.getUser().getId() + "");
			}else{
				ec.setValue("");
			}
		}else if(sv.equals("LOGIN_MANAGER_NAME")){
			Privilege privilege = (Privilege)session.getAttribute("privilege");
			if(privilege != null && privilege.getUser() != null){
				ec.setValue(privilege.getUser().getName());
			}else{
				ec.setValue("");
			}
		}*/
	}
}
